/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-06-20
 * Time: 9:10
 */
class Solution {
    public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            //右边找到不是val元素
            while(left<=right&&nums[right]==val){
                right--;
            }
            while(left<=right&&nums[left]!=val){
                left++;
            }
            if(left<=right){
                nums[left++] = nums[right--];
            }
        }
        return left;
    }
}
public class TestDemo {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums= {0,1,2,2,2,0,4,2};
        solution.removeElement(nums,2);
    }


}
